<template>
    <show-modal :title="title" :loading="loading" width="400px" @cancel="cancel" @submit="addSubmit">
        <a-form-model ref="form" :model="form" :rules="rules" v-bind="layout">
            <a-form-model-item label="功能编码" prop="funcCode">
                <a-input v-model.trim="form.funcCode" :disabled="disabled" placeholder="请填写功能编码" />
                <slot name="help">
                    <p class="help">功能编码会作为设备识别执行指令的Function属性</p>
                </slot>
            </a-form-model-item>
            <a-form-model-item label="功能名称" prop="funcName">
                <a-input v-model.trim="form.funcName" placeholder="请填写功能名称" />
            </a-form-model-item>
            <a-form-model-item label="产品属性" prop="attCode">
                <a-select v-model="form.attCode" show-search optionFilterProp="name" placeholder="请选择产品属性">
                    <a-select-option v-for="item in property" :name="item.attName+item.attCode" :value="item.attCode" :key="item.attCode">
                        {{ item.attName }}
                    </a-select-option>
                </a-select>
            </a-form-model-item>
        </a-form-model>
    </show-modal>
</template>

<script>
import { rules } from './rules';
import { addInfo, updateInfo } from '@/api/product/function';

export default {
    name: 'FunctionInfo',
    props: {
        productCode: String,
        info: Object,
        property: []
    },
    data() {
        return {
            loading: false,
            rules,
            readonly: [
                { label: '是', value: '1' },
                { label: '否', value: '0' }
            ],
            form: {
                funcCode: '',
                funcName: '',
                pdtCode: this.productCode,
                attCode: undefined
            },
            layout: {
                labelCol: { span: 5 },
                wrapperCol: { span: 19 }
            }
        };
    },
    computed: {
        title() {
            return this.info.id ? '修改功能' : '新增功能';
        },
        disabled() {
            return this.info?.id ? true : false;
        },
    },
    created() {
        this.form = { ...this.form, ...this.info };
    },
    methods: {
        //添加
        addSubmit() {
            this.$refs.form.validate(async valid => {
                if (valid) {
                    this.loading = true;
                    try {
                        if (this.form.id) {
                            await updateInfo(this.form);
                            this.$message.success('修改成功!');
                        } else {
                            await addInfo(this.form);
                            this.$message.success('添加成功!');
                        }
                        this.$emit('submit');
                    } finally {
                        this.loading = false;
                    }
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        cancel() {
            this.$emit('cancel');
        }
    }
};
</script>
<style lang="scss" scoped>
.help{
    line-height: 1;
    font-size: 12px;
    color:tomato;
}
</style>